package com.example.widget_lib.utils;

import com.example.widget_lib.ResourceTable;
import ohos.agp.components.Component;
import ohos.agp.render.Paint;
import ohos.agp.utils.Rect;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.Resource;
import ohos.global.resource.WrongTypeException;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.utils.PlainArray;
import ohos.utils.PlainBooleanArray;
import ohos.utils.PlainIntArray;
import ohos.utils.PlainLongArray;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.*;

/*
 * 工具类
 *
 */

public class Util {
    /**
     * Turns an array of colors (integer color values) into an ArrayList of
     * colors.
     *
     * @param colors
     * @return List<Integer> createColors
     */
    public static List<Integer> createColors(int[] colors) {
        List<Integer> result = new ArrayList<>();
        for (int i : colors) {
            result.add(i);
        }
        return result;
    }

    /**
     * 获取头像
     *
     * @param view
     * @param width
     * @return PixelMap
     */
    public static PixelMap getAvatar(Component view, int width) {
        Resource resource = null;
        try {
            resource = view.getResourceManager().getResource(ResourceTable.Media_hb);
            PixelMap mBitmap = ImageSource.create(resource, new ImageSource.SourceOptions()).createPixelmap(null);
//            Optional<PixelMap> optional = Optional.of(mBitmap);
//            int baseDensity = optional.get().getBaseDensity();
//            baseDensity = width;
            return mBitmap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
//        BitmapFactory.Options options = new BitmapFactory.Options();
//        options.inJustDecodeBounds = true;
//        BitmapFactory.decodeResource(resources, R.drawable.hb, options);
//        options.inJustDecodeBounds = false;
//        options.inDensity = options.outWidth;
//        options.inTargetDensity = width;
//        return BitmapFactory.decodeResource(resources, R.drawable.hb, options);

    }

    public static PixelMap getAvatar(Context context, int width) {
        try {
            String source = context.getResourceManager().getMediaPath(ResourceTable.Media_hb);
            ImageSource resources = ImageSource.create(source, new ImageSource.SourceOptions());

            ImageSource.DecodingOptions options = new ImageSource.DecodingOptions();
            options.allowPartialImage = true;
            resources.createPixelmap(ResourceTable.Media_hb, options);
            options.allowPartialImage = false;
            options.fitDensity = width;
            return resources.createPixelmap(ResourceTable.Media_hb, options);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        } catch (WrongTypeException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 资源ID转Bitmap
     *
     * @param view
     * @param resId
     * @param width
     * @return PixelMap
     */
    public static PixelMap getAvatar(Component view, int resId, int width) {
        Resource resource = null;
        try {
            resource = view.getResourceManager().getResource(resId);
            PixelMap mBitmap = ImageSource.create(resource, new ImageSource.SourceOptions()).createPixelmap(null);
//            Optional<PixelMap> optional = Optional.of(mBitmap);
//            int baseDensity = optional.get().getBaseDensity();
//            baseDensity = width;
            return mBitmap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }
//        BitmapFactory.Options options = new BitmapFactory.Options();
//        options.inJustDecodeBounds = true;
//        BitmapFactory.decodeResource(resources, resId, options);
//        options.inJustDecodeBounds = false;
//        options.inDensity = options.outWidth;
//        options.inTargetDensity = width;
//        return BitmapFactory.decodeResource(resources, resId, options);

    public static PixelMap getAvatar(Context context, int resId, int width) {
        String source = null;
        try {
            source = context.getResourceManager().getMediaPath(resId);
            ImageSource resources = ImageSource.create(source, new ImageSource.SourceOptions());
            ImageSource.DecodingOptions options = new ImageSource.DecodingOptions();
            options.allowPartialImage = true;
            resources.createPixelmap(resId, options);
            options.allowPartialImage = false;
            options.fitDensity = width;
            return resources.createPixelmap(resId, options);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        } catch (WrongTypeException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取文字区域宽度
     *
     * @param text  绘制的文字
     * @param paint 画笔
     * @return 文字的宽度
     */
    public static int getTextWidth(String text, Paint paint) {
        Rect bounds = paint.getTextBounds(text);
        int width = bounds.left + bounds.getWidth();
        return width;
    }

    /**
     * 获取文字区域高度
     *
     * @param text  绘制的文字
     * @param paint 画笔
     * @return 文字的高度
     */
    public static int getTextHeight(String text, Paint paint) {
        Rect bounds = paint.getTextBounds(text);
        int height = bounds.bottom + bounds.getHeight();
        return height;
    }

    /**
     * 判断对象是否为空
     *
     * @param obj 对象
     * @return {@code true}: 为空<br>{@code false}: 不为空
     */
    public static boolean isEmpty(Object obj) {
        if (obj == null) {
            return true;
        }
        if (obj instanceof String && obj.toString().length() == 0) {
            return true;
        }
        if (obj.getClass().isArray() && Array.getLength(obj) == 0) {
            return true;
        }
        if (obj instanceof Collection && ((Collection) obj).isEmpty()) {
            return true;
        }
        if (obj instanceof Map && ((Map) obj).isEmpty()) {
            return true;
        }
        if (obj instanceof PlainArray && ((PlainArray) obj).size() == 0) {
            return true;
        }
        if (obj instanceof PlainBooleanArray && ((PlainBooleanArray) obj).size() == 0) {
            return true;
        }
        if (obj instanceof PlainIntArray && ((PlainIntArray) obj).size() == 0) {
            return true;
        }
        if (obj instanceof PlainLongArray && ((PlainLongArray) obj).size() == 0) {
            return true;
        }
        return false;
    }

    /**
     * 判断对象是否非空
     *
     * @param obj 对象
     * @return {@code true}: 非空<br>{@code false}: 空
     */
    public static boolean isNotEmpty(Object obj) {
        return !isEmpty(obj);
    }


    /**
     * 获取文字的宽度和高度
     * 获取文字的宽度和高度
     *
     * @param text  绘制的文字
     * @param paint 画笔
     * @return 文字的宽高
     */
    public static int[] getTextWH(String text, Paint paint) {
        Rect bounds = paint.getTextBounds(text);
        int width = bounds.left + bounds.getWidth();
        int height = bounds.bottom + bounds.getHeight();
        return new int[]{width, height};
    }

    /**
     * 获取屏幕宽高
     *
     * @param context
     * @return int[]
     */
    public static int[] getScreenWH(Context context) {
        return new int[]{getDisplay(context).getAttributes().width, getDisplay(context).getAttributes().height};
    }

    /**
     * 获取z轴在location中的最佳位置
     * @param context
     * @return 获取z轴在location中的最佳位置
     */
    public static float getZForCamera(Context context) {
        return -6 * getDisplay(context).getAttributes().densityDpi;
    }


    public static Display getDisplay(Context context) {
        Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();
        return display;
    }
}
